package com.ctg.sysmgr.sso.server.data.dao.sys.impl;

import com.ctg.itrdc.cbs.common.dto.PageInfoDTO;
import com.ctg.itrdc.cbs.common.dto.QryCondDTO;
import com.ctg.itrdc.cbs.common.dto.RspBaseDTO;
import com.ctg.itrdc.cbs.staff.api.dto.QrySystemUserListDTO;
import com.ctg.itrdc.cbs.staff.api.dto.SystemUserDTO;
import com.ctg.itrdc.cbs.staff.api.dto.SystemUserDetailDTO;
import com.ctg.itrdc.cbs.staff.api.facade.ISystemUserQueryFacade;
import com.ctg.sysmgr.sso.server.common.uitl.StringToTimeStampUtil;
import com.ctg.sysmgr.sso.server.data.dao.sys.ISystemUserDao;
import com.ctg.sysmgr.sso.server.data.entity.SystemUser;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
public class SystemUserDubboImpl implements ISystemUserDao {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	private ISystemUserQueryFacade qrySystemUserList;
	private static final String DEFAULT_PATTERN = "yyyyMMddHHmmss";
	private String queryCode;
	private int pageIndex;
	private int pageSize;
	public String getQueryCode() {
		return queryCode;
	}

	public void setQueryCode(String queryCode) {
		this.queryCode = queryCode;
	}


	public int getPageIndex() {
		return pageIndex;
	}

	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public ISystemUserQueryFacade getQrySystemUserList() {
		return qrySystemUserList;
	}

	public void setQrySystemUserList(ISystemUserQueryFacade qrySystemUserList) {
		this.qrySystemUserList = qrySystemUserList;
	}

	@Override
	public SystemUser getUserByName(String username) {
		

		SystemUserDetailDTO stdo = this.getSystemUserDAO(queryCode, pageIndex, pageSize, username);
		SystemUserDTO systemUserDTO = stdo.getSystemUser();
		SystemUser systemUser = new SystemUser();
		try {
			transFormDTO(systemUser, systemUserDTO);
		} catch (Exception e) {
			logger.error("bean复制过程中发生错误" + e.getMessage());
			throw new RuntimeException(e.getMessage());
		}
		return systemUser;
	}

	
	
	private SystemUserDetailDTO getSystemUserDAO(String queryCode,int pageIndex,int pageSize,String queryParam){
		QrySystemUserListDTO qrySystemUserListDTO = new QrySystemUserListDTO();
		QryCondDTO queryCondition = new QryCondDTO();
		queryCondition.setQryType(queryCode); // 查询类型，用户名
		queryCondition.setQryValue1(queryParam); //
		qrySystemUserListDTO.setQueryCondition(queryCondition);// 设置条件
		PageInfoDTO pageInfo = new PageInfoDTO();
		pageInfo.setPageIndex(pageIndex);
		pageInfo.setPageSize(pageSize);
		qrySystemUserListDTO.setPageInfo(pageInfo);

		RspBaseDTO rspBaseDTO = null;

		try {
			rspBaseDTO = qrySystemUserList.qrySystemUserList(qrySystemUserListDTO);

		} catch (Exception e) {
			e.printStackTrace();
			logger.error("调用dubbo服务查询系统用户时出错: {}",queryCode + ":" + queryParam);
			logger.error(e.getMessage());
			throw new RuntimeException(e.getMessage());
		}
		List<SystemUserDetailDTO> systemUserDetailDTOList = (List<SystemUserDetailDTO>) rspBaseDTO.getRspResult();

		if (systemUserDetailDTOList == null || systemUserDetailDTOList.size() <= 0) {
			logger.info("调用dubbo服务,返回结果为空：{}",queryCode + ":" + queryParam);
			return null;
		}
		
		return systemUserDetailDTOList.get(0);
	}
	
	
	
	
	
	
	
	
	
	// system User对象属性转换
	private void transFormDTO(SystemUser su, SystemUserDTO s) {

		String[] ignore = { "createDate", "effDate", "expDate", "pwdNewtime", "statusDate", "updateDate" };
		BeanUtils.copyProperties(s, su, ignore);
		// 以下是日期属性
		su.setCreateDate(StringToTimeStampUtil.str2Timestamp(s.getCreateDate(), DEFAULT_PATTERN));// 当日期为空时，也设置为空
		su.setEffDate(StringToTimeStampUtil.str2Timestamp(s.getEffDate(),DEFAULT_PATTERN));
		su.setExpDate(StringToTimeStampUtil.str2Timestamp(s.getExpDate(),DEFAULT_PATTERN));
		su.setPwdNewtime(StringToTimeStampUtil.str2Timestamp(s.getPwdNewtime(),DEFAULT_PATTERN));
		su.setStatusDate(StringToTimeStampUtil.str2Timestamp(s.getStatusDate(),DEFAULT_PATTERN));
		su.setUpdateDate(StringToTimeStampUtil.str2Timestamp(s.getUpdateDate(),DEFAULT_PATTERN));

		/*// 以下是一般属性
		su.setCommonRegionId(Integer.parseInt(s.getCommonRegionId() + ""));
		su.setCreateStaff(s.getCreateStaff());

		su.setId(s.getSysUserId());// id=sysUserId
		su.setLimitCount(Integer.parseInt(s.getLimitCount() + ""));
		su.setLoginedNum(Integer.parseInt(s.getLoginedNum() + ""));
		su.setPassword(s.getPassword());
		su.setPwdEffectDays(Integer.parseInt(s.getPwdEffectDays() + ""));
		su.setPwdErrCnt(Integer.parseInt(s.getPwdErrCnt() + ""));

		su.setPwdSmsTel(Long.parseLong(s.getPwdSmsTel()));
		su.setPwdStatus(s.getPwdStatus());
		su.setStaffId(s.getStaffId());
		su.setStatusCd(s.getStatusCd());

		su.setSysCode(s.getSysCode());
		su.setSysUserCode(s.getSysUserCode());
		su.setSysUserDesc(s.getSysUserDesc());
		su.setSysUserId(s.getSysUserId());
		su.setTenantId(s.getTenantId());

		su.setUpdateStaff(s.getUpdateStaff());*/

	}


}
